/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: vosao.dev@gmail.com
 */

package org.vosao.plugins.csdata.service;

//import java.util.Collections;
import java.util.Date;
//import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

//import net.tanesha.recaptcha.ReCaptchaResponse;

//import org.apache.commons.lang.StringUtils;
//import org.apache.velocity.VelocityContext;
import org.vosao.business.Business;
//import org.vosao.common.PluginException;
//import org.vosao.entity.ConfigEntity;
//import org.vosao.entity.UserEntity;
import org.vosao.i18n.Messages;

import org.vosao.plugins.csdata.dao.CsdataDao;
//import org.vosao.plugins.csdata.entity.CsdataConfigEntity;
import org.vosao.plugins.csdata.entity.CsdataUserEntity;
import org.vosao.service.ServiceResponse;
//import org.vosao.utils.EmailUtil;
import org.vosao.utils.ParamUtil;
//import org.vosao.utils.RecaptchaUtil;
//import org.vosao.utils.StrUtil;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class CsdataFrontServiceImpl extends AbstractCsdataService 
		implements CsdataFrontService {

	public CsdataFrontServiceImpl(Business business, CsdataDao aCsdataDao) {
		super(business, aCsdataDao);
	}

	@Override
	public ServiceResponse register(Map<String, String> vo, 
			String challenge, String response, HttpServletRequest request) {
		//CsdataConfigEntity csdataConfig = getCsdataDao().getCsdataConfigDao()
		//		.getConfig();
		UserService userService = UserServiceFactory.getUserService();
		logger.info(userService.getCurrentUser().getEmail());
		CsdataUserEntity reg = getCsdataDao().getCsdataUserDao()
				.getByEmail(userService.getCurrentUser().getEmail());
		if (reg == null) {	
			reg = new CsdataUserEntity();
		}
		reg.setCreatedDate(new Date());
		//reg.setData(ParamUtil.filterXSS(vo.get("data")));
		reg.setData(vo.get("data"));
		reg.setEmail(userService.getCurrentUser().getEmail());
		logger.info(reg.toString());
		//reg.setName(ParamUtil.filterXSS(vo.get("name")));
		//reg.setPassword(vo.get("password1"));
		//reg.createSessionKey();
		//UserEntity user = getDao().getUserDao().getByEmail(reg.getEmail());
		//if (user != null) {
		//	return ServiceResponse.createErrorResponse(Messages.get(
		//			"register.user_exists", reg.getEmail()));
		//}
		getCsdataDao().getCsdataUserDao().save(reg);
		//try {
			//sendConfirmLetter(reg);
			return ServiceResponse.createSuccessResponse(
					Messages.get("success"));
		//}
		//catch (PluginException e) {
		//	e.printStackTrace();
		//	return ServiceResponse.createErrorResponse(e.getMessage());
		//}
	}
	
	public CsdataUserEntity getCsdataUser() {
		try {
			UserService userService = UserServiceFactory.getUserService();	
			return getCsdataDao().getCsdataUserDao().getByEmail(userService.getCurrentUser().getEmail());
		}
		catch(Exception e) {
			e.printStackTrace();
			return new CsdataUserEntity(); //Collections.EMPTY_LIST;
		}
	}


	
	
	
}
